package com.xrh.xdemo.service.service;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xrh.core.bean.CommonPage;
import com.xrh.core.bean.CommonResult;
import com.xrh.core.exceptions.BusinessException;
import com.xrh.xdemo.api.dto.extend.OperateTraceDTO;
import com.xrh.xdemo.api.request.queryRequest.OperateTraceQueryRequest;
import com.xrh.xdemo.service.entity.OperateTraceEntity;
import com.xrh.xdemo.service.mapper.OperateTraceMapper;

import cn.hutool.core.bean.BeanUtil;

@Service
public class OperateTraceService {

	@Resource
	OperateTraceMapper operateTraceMapper;
	
	/**
	 * @function 新增操作痕迹 - 异步新增
	 * @author 肖荣辉
	 * @date 2024年1月5日
	*/
	@Async
	public void addOperateTrace(OperateTraceDTO dto) {
		
		OperateTraceEntity operateTraceEntity = BeanUtil.copyProperties(dto, OperateTraceEntity.class, "id");
		
		operateTraceMapper.insert(operateTraceEntity);
	}
	
	
	/**
	 * @function 删除OperateTrace
	 * @param id
	 * @author 肖荣辉
	 * @date 2024-01-05 16:19:58
	*/
	public void deleteOperateTraceById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
			
		OperateTraceEntity operateTraceEntity = operateTraceMapper.selectById(id);
		
		if(operateTraceEntity == null)
			throw new BusinessException("id不合法");
		
		UpdateWrapper<OperateTraceEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", id);
		
		//因为注解了@TableLogic , 自动会把删除标志位设置为1 
		operateTraceMapper.delete(updateWrapper);
		
	}

	/**
	 * @function 根据ID查询OperateTrace Entity
	 * @param id
	 * @author 肖荣辉
	 * @date 2024-01-05 16:19:58
	*/
	public OperateTraceEntity findOperateTraceEntityById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
			
		OperateTraceEntity operateTraceEntity = operateTraceMapper.selectById(id);
		
		return operateTraceEntity;
	}
	
	/**
	 * @function 根据ID查询OperateTrace DTO
	 * @param id
	 * @author 肖荣辉
	 * @date 2024-01-05 16:19:58
	*/
	public OperateTraceDTO findOperateTraceById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
	
		OperateTraceEntity operateTraceEntity = operateTraceMapper.selectById(id);
		
		OperateTraceDTO  operateTraceDTO = BeanUtil.toBean(operateTraceEntity , OperateTraceDTO.class);
		
		return operateTraceDTO;
	}

	/**
	 * @function 根据OperateTraceQueryVo查询OperateTrace列表
	 * @param operateTraceQueryRequest
	 * @author 肖荣辉
	 * @date 2024-01-05 16:19:58
	*/
	public CommonResult<CommonPage<OperateTraceDTO>> findOperateTraceList(OperateTraceQueryRequest operateTraceQueryRequest) {
		
		Page<OperateTraceEntity> page = operateTraceQueryRequest.createPage();

		QueryWrapper<OperateTraceEntity> queryWrapper = operateTraceQueryRequest.createQuery();
		
		queryWrapper.orderByDesc("operate_time");
		
		operateTraceMapper.selectPage(page, queryWrapper);
		
		List<OperateTraceDTO> dtoList = BeanUtil.copyToList(page.getRecords() , OperateTraceDTO.class);
		
		return CommonResult.success(CommonPage.create(operateTraceQueryRequest.getPageNum() , operateTraceQueryRequest.getPageSize() , dtoList, page.getTotal()));
	}
	
	
	
}
